/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/binary-tree-right-side-view
   @Language: C++
   @Datetime: 20-01-06 11:52
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
	vector<int> rightSideView(TreeNode* root) {
		vector<int> rights;
		if(root==NULL) return rights;
		queue<TreeNode*> q;
		for(q.push(root); q.size();){
			TreeNode *r=NULL;
			for(int size=q.size(); size--; q.pop()){
				r = q.front();
				if(r->left) q.push(r->left);
				if(r->right)q.push(r->right);
			}
			if(r) rights.push_back(r->val);
		}
		return rights;
	}
};
